﻿package ${Package}.bo.impl;
import ${Package}.beans.ConstVar;
import ${Package}.beans.ValueText;
import ${Package}.dao.BaseDao;
import ${Package}.dao.ext.SysEnumDao;
import ${Package}.dao.ext.${table.TableNameAlias}Dao;
import ${Package}.model.BaseModel;
import ${Package}.model.SysEnum;
import ${Package}.model.${table.TableNameAlias};
import ${Package}.utils.ConfigUtils;
import ${Package}.utils.EhCacheUtil;
import ${Package}.utils.ReturnResult;
import ${Package}.utils.Status;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

	#foreach($c in $ColumnList)
	#if($c.IsPrimary)
	#set ($primaryKeyAlias=$c.ColumnNameAlias)
	#set ($primaryKey=$c.ColumnName)
	#end
    #end
/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:${Utils.GetDate} ${Utils.GetTime}
*/ 
public class ${table.TableNameAlias}Bo  extends BaseBoImp{
    
	Logger log = Logger.getRootLogger();
	/**
	*批量删除数据
	*/
    public ReturnResult delByIds(String ids) {
        SqlSession session = getSqlSession();
		ReturnResult result=new ReturnResult();
		try{
			${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
			result= delByMap(dao,ids);
        }
		catch(Exception ex)
		{
			log.error(ex.getMessage());
		}
		finally{
			session.close();
		}
        return result;
    }
	    /**
     * 分页数据查询
     * @param pageIndex
     * @param pageSize
     * @param map
     * @return
     */
    public ReturnResult selectByMap(int pageIndex, int pageSize,Map<String, Object> map) {
        SqlSession session = getSqlSession();
        ${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
        int recordNum=(pageIndex-1)*pageSize;
        List<${table.TableNameAlias}> lst = session.selectList(ConfigUtils.getInstance().getDaoPackage(".${table.TableNameAlias}Dao.selectByMap"), map, new RowBounds(recordNum, pageSize)) ;
        int total =dao.count(map);
        ReturnResult result = new ReturnResult();
        result.setCode(100001000);
        result.setRows(lst);
        result.setTotal(total);
        result.setStatus(Status.SUCCESS);
        session.close();
        return result;
    }
	@Override
    public ReturnResult insertByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        ${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
        ReturnResult result= insertByModel(dao,model);
        session.close();
        return result;
    }
    @Override
    public ReturnResult editByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        ${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
        ReturnResult result = editByModel(dao,model);
        session.close();
        return result;
    }
	    /**
     * 根据ID获取一条数据
     *
     * @param id
     * @return
     */
    public ReturnResult selectById(Integer id) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        result.setStatus(Status.ERROR);
        BaseDao dao = session.getMapper(${table.TableNameAlias}Dao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        List<${table.TableNameAlias}> lst = dao.selectByMap(map);
        if (lst.size() == 1) {
            result.setData(lst.get(0));
            result.setStatus(Status.SUCCESS);
        } else {
            result.setCode(100004000);
        }
        session.close();
        return result;
    }
	/**
	*批量审核数据
	*
	*/
    public ReturnResult audit(int state,String[] idArr) {
        SqlSession session = getSqlSession();
        ${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("idArr", idArr);
        map.put("state", state);
        Integer num = dao.audit(map);
        ReturnResult result = new ReturnResult();
        if (num > 0) {
            result.setCode(100001000);
            result.setStatus(Status.SUCCESS);
        }
        session.close();
        return result;
    }

    /**
     * 排序
     *
     * @param sorts
     * @param ids
     * @return
     */
    public ReturnResult sort(String sorts, String ids) {
        ReturnResult result;
        SqlSession session=getSqlSession();
        ${table.TableNameAlias}Dao dao=session.getMapper(${table.TableNameAlias}Dao.class);
         result=  sort(session,dao,sorts,ids);
        if (session!=null)
        session.close();
        return result;
    }
}